package com.chaopei.myexercise;

/**
 * @Author Corday 天涯
 * @Date 2022/3/14 19:08
 */
public class MyRelay implements Runnable {
    //几个人共同跑1000米
    private int distance = 1000;
    @Override
    public  void run() {
       while (true){
           if(distance<=100){
               break;
           }
           //走你
           go();
       }
    }
    public synchronized   void go(){//保证是一个人跑步100米
//        一人跑100米
        for(int i=0;i<100;i+=10){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"跑了:"+(i+10)+"米");
        }
//     跑完100米后减距离
    distance -=100;
    }
}//myrelay
//测试一下
class  RelayTest{
    public static void main(String[] args) {
        MyRelay myRelay = new MyRelay();
        for(int i=1;i<=5;i++){
            new Thread(myRelay,"选手"+i).start();
        }
//        new Thread(myRelay,"选手1").start();
//        new Thread(myRelay,"选手2").start();
//        new Thread(myRelay,"选手3").start();
//        new Thread(myRelay,"选手4").start();
//        new Thread(myRelay,"选手5").start();
    }
}